package com.loe.ui.page

import android.app.Activity
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import com.loe.mvp.BaseDialog
import com.loe.mvp.ext_app.color
import com.loe.mvp.ext_java.AsyncTimer
import com.loe.mvp.ext_java.safe
import com.loe.mvp.ext_java.timer
import com.loe.mvp.ext_view.visible
import com.loe.ui.LoeUI
import com.loe.ui.R
import com.loe.ui.UiSpMain
import com.loe.ui.openUrl
import kotlinx.android.synthetic.main.activity_ui.*
import kotlinx.android.synthetic.main.dialog_ui.view.*

class UiDialog(activity: Activity, val jsClass: String) : BaseDialog(activity, R.layout.dialog_ui)
{
    private var loop: AsyncTimer? = null

    override fun ViewGroup.onView()
    {
        viewConsole.visible = LoeUI.isPreview

        setAnimation()

        // 加载过了
        if (UiSpMain.md5.isNotEmpty())
        {
            init()
        } else
        {
            // 加载
            LoeUI.loadPackJs()
            {
                init()
            }
        }

        initEvent()

        dialog.setOnCancelListener()
        {
            safe { uiPage.runJsFun("onDismiss") }
            loop?.finish()
            dismiss()
        }
    }

    /** 初始化page */
    fun init()
    {
        rootView.uiPage.init(jsClass)
        {
            val page = this@UiDialog
            it.appendJavascriptInterface(object
            {
                @JavascriptInterface
                fun toast(msg: String?)
                {
                    page.toast(msg)
                }

                @JavascriptInterface
                fun showLoading(msg: String?)
                {
                    page.showLoading(msg ?: "")
                }

                @JavascriptInterface
                fun cancelLoading()
                {
                    page.cancelLoading()
                }

                @JavascriptInterface
                fun dismiss()
                {
                    if(dialog.isShowing)
                    {
                        safe { rootView.uiPage.runJsFun("onDismiss") }
                        page.dismiss()
                    }
                }

                @JavascriptInterface
                fun openUrl(urlString: String)
                {
                    page.openUrl(urlString)
                }

                @JavascriptInterface
                fun openDialog(jsClass: String)
                {
                    UiDialog(activity, jsClass).show()
                }

                @JavascriptInterface
                fun setCancelable(cancelable: Boolean)
                {
                    dialog.setCancelable(cancelable)
                }

                @JavascriptInterface
                fun setCanceledOnOutside(canceledOnOutside: Boolean)
                {
                    dialog.setCanceledOnTouchOutside(canceledOnOutside)
                }
            })
        }
    }

    /******************************************* test *************************************/

    override fun loadData()
    {
        LoeUI.loadPackJs()
        {
            init()
        }
    }

    override fun initEvent()
    {
        rootView.buttonRefresh.setOnClickListener()
        {
            loadData()
        }

        rootView.buttonLoop.setOnClickListener()
        {
            if (loop != null)
            {
                loop?.finish()
                loop = null
                rootView.buttonLoop.setTextColor(activity.color(R.color.white))
            } else
            {
                loop = timer(0, 500)
                {
                    LoeUI.loadPackJs()
                    {
                        if(it) init()
                    }
                }
                rootView.buttonLoop.setTextColor(activity.color(R.color.green))
            }
        }
        if(LoeUI.isPreview) rootView.buttonLoop.performClick()

        rootView.buttonOpen.setOnClickListener()
        {
            UiDialog(activity, jsClass).show()
        }
    }
}